#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> map;
        for(int i = 0; i < nums.size(); i++){
            auto iter = map.find(target - nums[i]);
            if(iter != map.end()){ // 如果 target - nums[i] 被找到，则返回nums[i]的索引和当前 target - nums[i] 的索引
                return {iter->second, i}; 
            }
            else map.insert(pair<int, int>(nums[i], i)); // 添加当前值及其索引，如{2:0, 7:1}
        }
        return {};
    }
};

int main()
{
    Solution s;
    vector<int> nums = {2, 7, 11, 15};
    int target = 9;
    vector<int> result = s.twoSum(nums, target);
    for(int num : result){
        cout << num << " ";
    }
    return 0;
}